package com.resou.reader.discovery.choicenes;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.assist.JavaScriptInterface;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.discovery.choicenes.fragment.FragmentBoy;
import com.resou.reader.discovery.choicenes.fragment.FragmentGril;
import com.resou.reader.mine.adapter.ViewPagerAdapter;
import com.resou.reader.utils.eventbus.MessageEvent;
import com.resou.reader.view.CustomTitleView;
import com.resou.reader.view.booklebel.BookLabelView;
import com.resou.reader.view.booklebel.ILebelCallback;
import com.resou.reader.view.booklebel.LabelBean;
import com.resou.reader.view.booklebel.LabelNameBean;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChoiceneBookActivity extends ResouBaseActivity<ChoiceneBookPresenter> implements IChoiceneBookContract.IChoiceneBookView, ViewPager.OnPageChangeListener {

    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mMagicIndicator)
    MagicIndicator mMagicIndicator;
    @BindView(R.id.tvScreen)
    TextView tvScreen;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.bvBookLabelView)
    BookLabelView bvBookLabelView;
    List<String> list = null;
    List<Fragment> fragmentList;
    FragmentBoy fragmentBoy;
    FragmentGril fragmentGril;
    int gender = 1;

    /**
     * 精品书单
     *
     * @param context
     */
    public static void start(Context context) {
        Intent starter = new Intent(context, ChoiceneBookActivity.class);
        context.startActivity(starter);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.anim_detail_enter, R.anim.anim_no);
        }
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new ChoiceneBookPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("精选书单");
        list = new ArrayList<>();
        list.add("男生");
        list.add("女生");
        fragmentBoy = FragmentBoy.newInstance();
        fragmentGril = FragmentGril.newInstance();
        fragmentList = new ArrayList<>();
        fragmentList.add(fragmentBoy);
        fragmentList.add(fragmentGril);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(viewPagerAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(couponAdapter);
        mMagicIndicator.setNavigator(commonNavigator);

        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
        mViewPager.addOnPageChangeListener(this);
        bvBookLabelView.setILebelCallback(new ILebelCallback() {
            @Override
            public void setSaveOnclick(String type, String category, String label) {
                MessageEvent event = new MessageEvent();
                event.setLabel(label);
                event.setCategory(category);
                event.setGender(gender + "");
                event.setType(type);
                EventBus.getDefault().post(event);
            }

        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_choicene_book;
    }

    @Override
    public void retryRefreshData() {
        mPresenter.quearLabel(gender);
        MessageEvent event = new MessageEvent();
        event.setLabel("");
        event.setCategory("");
        event.setGender("1");
        event.setType("");
        EventBus.getDefault().post(event);
        MessageEvent event2 = new MessageEvent();
        event2.setLabel("");
        event2.setCategory("");
        event2.setGender("2");
        event2.setType("");
        EventBus.getDefault().post(event2);
    }

    @OnClick({R.id.ivBack, R.id.tvScreen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvScreen:
                if (bvBookLabelView.isShow()) {
                    bvBookLabelView.onHidden();
                } else {
                    bvBookLabelView.onShow();
                }
                break;
        }
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        bvBookLabelView.clear();
        mMagicIndicator.onPageSelected(i);
        if (bvBookLabelView.isShow()) {
            bvBookLabelView.onHidden();
        }
        if (i == 0) {
            gender = 1;
            MessageEvent event = new MessageEvent();
            event.setGender("3");
            EventBus.getDefault().post(event);
        } else {
            gender = 2;
            MessageEvent event = new MessageEvent();
            event.setGender("4");
            EventBus.getDefault().post(event);
        }
        mPresenter.quearLabel(gender);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }


    /**
     * 头部菜单栏适配器
     */
    CommonNavigatorAdapter couponAdapter = new CommonNavigatorAdapter() {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public IPagerTitleView getTitleView(Context context, int i) {
            CustomTitleView customTitleView = new CustomTitleView(context);
            customTitleView.setNormalColor(context.getResources().getColor(R.color.color_ff333333));
            customTitleView.setSelectedColor(context.getResources().getColor(R.color.black));
            customTitleView.setText(list.get(i));
            customTitleView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(i);
                }
            });
            return customTitleView;
        }

        @Override
        public IPagerIndicator getIndicator(Context context) {
            LinePagerIndicator indicator = new LinePagerIndicator(context);
            indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
            indicator.setColors(context.getResources().getColor(R.color.theme_color));
            indicator.setLineHeight(6);
            return indicator;
        }
    };

    @Override
    public void initData() {
        mPresenter.quearLabel(gender);
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        dissLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {
    }

    @Override
    public void showLabel(List<BookLabelBean> list) {
        bvBookLabelView.setData(list);
    }
}
